package com.gator.spring.framework.aop.aspect;

import com.gator.spring.framework.aop.intercept.PABLO_MethodInterceptor;
import com.gator.spring.framework.aop.intercept.PABLO_MethodInvocation;

import java.lang.reflect.Method;

/**
 * @Author PABLO
 * @Date 2022/5/8 17:17
 * @Desc 前置通知拦截器  在执行拦截器链的时候，会依次回调这里的invoke方法
 */
public class PABLO_MethodBeforeAdviceInterceptor
        extends PABLO_AbstractAspectAdvice
        implements PABLO_Advice, PABLO_MethodInterceptor {

    //切点  切点连接点，即每个method和它对应的拦截器链
    private PABLO_JoinPoint joinPoint;

    public PABLO_MethodBeforeAdviceInterceptor(Method aspectMethod, Object aspectTarget) {
        super(aspectMethod, aspectTarget);
    }

    private void before(Method method,Object[] args,Object target) throws Throwable{
        //传送了给织入参数
        //method.invoke(target);
        super.invokeAdviceMethod(this.joinPoint,null,null);

    }


    //被com.gator.spring.framework.aop.intercept.PABLO_MethodInvocation.proceed回调
    //执行完毕后
    //在回调到com.gator.spring.framework.aop.intercept.PABLO_MethodInvocation.proceed继续执行
    @Override
    public Object invoke(PABLO_MethodInvocation invocation) throws Throwable {
        //从被织入的代码中才能拿到，JoinPoint
        this.joinPoint = invocation;
        //先执行本节点自己添加的逻辑，即method的某一个拦截器节点
        before(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
        //执行完毕后在回调到com.gator.spring.framework.aop.intercept.PABLO_MethodInvocation.proceed
        return invocation.proceed();
    }
}
